package com.mubolg.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import com.aliyun.credentials.utils.AuthUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mubolg.constants.Constants;
import com.mubolg.constants.GlobalExceptionCode;
import com.mubolg.mapper.PermissionMapper;
import com.mubolg.pojo.domain.Login;
import com.mubolg.mapper.LoginMapper;
import com.mubolg.pojo.domain.LoginInfoDto;
import com.mubolg.pojo.domain.Permission;
import com.mubolg.pojo.param.RegisterSaveLoginParam;
import com.mubolg.pojo.vo.LoginVo;
import com.mubolg.properties.SaTokenSettingProperties;
import com.mubolg.service.ILoginService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mubolg.template.AppWeChatTemplate;
import com.mubolg.utils.AssertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 登录表 服务实现类
 * </p>
 *
 * @author YuGeGe
 * @since 2024-08-01
 */
@Service
public class LoginServiceImpl extends ServiceImpl<LoginMapper, Login> implements ILoginService {
    @Autowired
    private AppWeChatTemplate appWeChatTemplate;
    @Autowired
    private PermissionMapper permissionMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private SaTokenSettingProperties saTokenSettingProperties;
    /**
     * 微信登录
     * @param code
     * @return
     */
    @Override
    public LoginVo wxlogin(String code , Integer type) {
        // 获取openId
        String openID = appWeChatTemplate.getOpenID(code);
        // 判断openId是否存在
        Login login = super.getOne(new LambdaQueryWrapper<Login>().eq(Login::getOpenId,openID).eq(Login::getType,type));
        // 存在则抛出异常
        AssertUtil.isNotNull(login,GlobalExceptionCode.PARAM_PHONE_EXIST);

        // 业务实现
        // 获取登录id
        Long loginId = login.getId();
        // 使用sa-token进行登录
        StpUtil.login(loginId);
        // 获取token名称
        String tokenName = StpUtil.getTokenName();
        // 获取token
        String tokenValue = StpUtil.getTokenValue();
        // 查询当前的用户权限信息
        List<String> permissions = permissionMapper.selectByLoginId(loginId);
        // 拼接一个key
        String key = String.format(saTokenSettingProperties.getKey(), loginId);
        // 存入redis
        redisTemplate.opsForValue().set(key,permissions,StpUtil.getSessionTimeout(), TimeUnit.SECONDS);
        // 封装返回值
        LoginInfoDto loginInfoDto = BeanUtil.copyProperties(login, LoginInfoDto.class);
        // 返回值
        return new LoginVo(tokenName,tokenValue,permissions,loginInfoDto);
    }

    /**
     * 保存登录信息
     * @param registerSaveLoginParam
     */
    @Override
    public void registerSaveLogin(RegisterSaveLoginParam registerSaveLoginParam) {
        Login login = new Login();
        // 属性拷贝
        BeanUtil.copyProperties(registerSaveLoginParam,login,false);
        login.setCreateTime(new Date());
        login.setVersion(0);
        login.setDeleted(false);
        super.save(login);
    }


}
